'use strict';
layui.define(['jquery','layer','form'], function(exports){
    let $ = layui.jquery
        ,form = layui.form
        ,layer= layui.layer;

    let obj = {
        //文章id
        articleId:0,
        //form表单
        filter: '',
        //评论Url
        formUrl: '',
        delUrl: '',

        //显示回复按钮
        replyBtn: '',
        delBtn: '',
        //回复表单
        replyText: '',
        replySubmit: '',
        //评论外部
        box:null,
        tpl:'',

        post: null,

        //初始化
        init: function(obj){
            this.articleId = obj.articleId;
            this.filter = obj.filter;
            this.formUrl = obj.formUrl;
            this.delUrl = obj.delUrl;

            this.replyBtn = obj.replyBtn;
            this.delBtn = obj.delBtn;
            this.replyText = obj.replyText;
            this.replySubmit = obj.replySubmit;
            this.box = obj.box;
            this.tpl = obj.tpl;
            this.post = obj.post;


            //注册事件
            this.event();
        },
        //绑定事件
        event: function(){
            let that = this
                ,body = $('body');

            //品论事件
            form.on('submit('+this.filter+')', function(data){
                that.submit($.extend(data.field, {article_id: that.articleId,parent_id:0}));
                //清空文本框
                $('textarea[name="comment"]').val('');
                return false;
            });

            //显示回复框
            body.on('click', that.replyBtn, function(){
                //删除回复框
                $(that.replySubmit).parent().remove();
                $(this).closest(that.box).append(that.tpl);
            });

            //提交回复
            body.on('click', that.replySubmit, function(){
                let comment = $(that.replyText).val()
                    ,parent_id = $(this).closest('.comment-id').data('id');
                that.submit({
                    article_id:that.articleId,
                    parent_id:parent_id,
                    comment:comment
                });
                //清空文本框
                $(that.replyText).val('');

            });

            //删除
            body.on('click', that.delBtn, function(){
                let commentId = $(this).closest('li').data('id');

                layer.confirm('您确定要删除吗？', {
                    btn: ['删除','取消']
                }, function(){
                    that.delete({comment_id:commentId, article_id:that.articleId});
                });
            });
        },
        //删除操作
        delete: function(data){
            let that = this;
            $.ajax({
                url:that.delUrl,
                type:'POST',
                data:data,
                success: function(d){
                    layer.msg(d.msg, function(){
                        if (d.code === 0){
                            that.refresh(data);
                        }
                        console.log(d.msg);
                    });
                }
            });
        },
        //提交评论
        submit: function(data){
            let that = this;
            $.ajax({
                url:that.formUrl,
                type: 'POST',
                data: data,
                success: function(d){
                    layer.msg(d.msg, function(){
                        if (d.code === 0){
                            that.refresh(data);
                        }
                        console.log(d.msg);
                    });
                }
            });
        },
        //刷新页码
        refresh: function(data){
            this.post.cache = {};
            if(data.parent_id === 0)
            //操作的评论 刷新到第一页
                this.post.refresh({curr:1});
            else
            //操作的是回复 刷新当前页
                this.post.refresh();
        }
    };


    //输出test接口
    exports('comment', obj);
});